home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xpaint-2.1.1 / main.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  8KB  |  324 lines

  1. /* +-------------------------------------------------------------------+ */
  2. /* | Copyright 1992, 1993, David Koblas (koblas@netcom.com)            | */
  3. /* |                                                                   | */
  4. /* | Permission to use, copy, modify, and to distribute this software  | */
  5. /* | and its documentation for any purpose is hereby granted without   | */
  6. /* | fee, provided that the above copyright notice appear in all       | */
  7. /* | copies and that both that copyright notice and this permission    | */
  8. /* | notice appear in supporting documentation.  There is no           | */
  9. /* | representations about the suitability of this software for        | */
  10. /* | any purpose.  this software is provided "as is" without express   | */
  11. /* | or implied warranty.                                              | */
  12. /* |                                                                   | */
  13. /* +-------------------------------------------------------------------+ */
  14.  
  15. #include <X11/Intrinsic.h>
  16. #include <X11/Shell.h>
  17. #include <X11/StringDefs.h>
  18. #include <stdio.h>
  19. #include <fcntl.h>
  20. #ifndef NOSTDHDRS
  21. #include <unistd.h>
  22. #endif
  23.  
  24. #define DEFINE_GLOBAL
  25. #include "xpaint.h"
  26. #include "misc.h"
  27.  
  28. #include "XPaintIcon.xpm"
  29.  
  30. static char *appDefaults[] = {
  31. #include "XPaint.ad.h"
  32.     NULL
  33. };
  34.  
  35. typedef struct {
  36.     String    visualType;
  37.     String    size;
  38.     String    help;
  39.     String    rcFile;
  40.     Boolean    popped;
  41. } AppInfo;
  42.  
  43. static AppInfo    appInfo;
  44.  
  45. static XtResource resources[] = {
  46.     { "visualType", "VisualType", XtRString, sizeof(String),
  47.        XtOffset(AppInfo*, visualType), XtRImmediate, (XtPointer)"default" },
  48.     { "size", "Size", XtRString, sizeof(String),
  49.        XtOffset(AppInfo*, size), XtRImmediate, (XtPointer)"640x480" },
  50.     { "rcFile", "RcFile", XtRString, sizeof(String),
  51.        XtOffset(AppInfo*, rcFile), XtRImmediate, (XtPointer)NULL },
  52.     { "help", "Help", XtRString, sizeof(String),
  53.        XtOffset(AppInfo*, help), XtRImmediate, (XtPointer)NULL },
  54.     { "popped", "Popped", XtRBoolean, sizeof(Boolean),
  55.        XtOffset(AppInfo*, popped), XtRImmediate, (XtPointer)False },
  56. };
  57.  
  58. static XrmOptionDescRec options[] = {
  59.     { "-24",     ".visualType", XrmoptionNoArg,  (XtPointer)"true24" },
  60.     { "-12",     ".visualType", XrmoptionNoArg,  (XtPointer)"cmap12" },
  61.     { "-size",   ".size",       XrmoptionSepArg, (XtPointer)NULL },
  62.     { "-rcFile", ".rcFile",     XrmoptionSepArg, (XtPointer)NULL },
  63.     { "-popped", ".popped",     XrmoptionNoArg,  (XtPointer)"True" },
  64.     { "-help",   ".help",       XrmoptionNoArg,  (XtPointer)"command" },
  65.     { "+help",   ".help",       XrmoptionSepArg, (XtPointer)NULL },
  66. };
  67.  
  68. void BrushInit(Widget);
  69. void HelpInit(Widget);
  70. void OperationInit(Widget);
  71. void InitTypeConverters();
  72.  
  73. /*
  74. **  Public query functions for application defaults
  75. */
  76.  
  77. void GetDefaultWH(int *w, int *h)
  78. {
  79.     int        x, y;
  80.     unsigned int    width, height;
  81.  
  82.     XParseGeometry(appInfo.size, &x, &y, &width, &height);
  83.     *w = width;
  84.     *h = height;
  85. }
  86.  
  87. char *GetDefaultRC()
  88. {
  89.     return appInfo.rcFile;
  90. }
  91.  
  92. /*
  93. **  Create a nice icon image for XPaint...
  94. */
  95. void SetIconImage(Widget w, Boolean flag)
  96. {
  97.     static Pixmap    icon = None;
  98.     static int    iconW = 1, iconH = 1;
  99.     Window        iconWindow;
  100.     XWMHints    wmHints;
  101.     Screen        *screen = XtScreen(w);
  102.     Display        *dpy = XtDisplay(w);
  103.  
  104. #if 0
  105.     int        i, count;
  106.     XIconSize    *list;
  107.  
  108.     /*
  109.     **  Should use this information
  110.     */
  111.  
  112.     XGetIconSizes(dpy, RootWindowOfScreen(screen), &list, &count);
  113.     printf("got %d sizes\n", count);
  114.     for (i = 0; i < count; i++) {
  115.         printf("#%d:  min = %d,%d  max = %d,%d  inc = %d,%d\n", i, 
  116.             list[i].min_width, list[i].min_height,
  117.             list[i].max_width, list[i].max_height,
  118.             list[i].width_inc, list[i].height_inc);
  119.     }
  120.     XFree((XPointer)list);
  121. #endif
  122.  
  123.     /*
  124.     **  Build the XPaint icon
  125.     */
  126.     iconWindow = XCreateSimpleWindow(dpy, RootWindowOfScreen(screen),
  127.                         0, 0, /* x, y */
  128.                         iconW, iconH, 0,
  129.                         BlackPixelOfScreen(screen),
  130.                         BlackPixelOfScreen(screen));
  131.     if (icon == None) {
  132.         XpmCreatePixmapFromData(dpy, iconWindow,
  133.                 XPaintIcon_xpm, &icon, NULL, NULL);
  134.         GetPixmapWHD(dpy, icon, &iconW, &iconH, NULL);
  135.         XResizeWindow(dpy, iconWindow, iconW, iconH);
  136.     }
  137.  
  138.     XSetWindowBackgroundPixmap(dpy, iconWindow, icon);
  139.  
  140.     XtVaSetValues(w, XtNiconWindow, iconWindow, NULL);
  141. }
  142.  
  143.  
  144. /*
  145. **  The rest of main
  146. */
  147. extern void HelpTextOutput(FILE*, char *);
  148.  
  149. static void usage(char *prog, char *msg)
  150. {
  151.     if (msg)
  152.         fprintf(stderr,"%s\n", msg);
  153.     fprintf(stderr,"Usage: %s\n", prog);
  154.     HelpTextOutput(stderr, appInfo.help == NULL ? "command" : appInfo.help);
  155.     exit(1);
  156. }
  157.  
  158. typedef struct {
  159.     XtWorkProcId    id;
  160.     Widget        toplevel;
  161.     int        nfiles;
  162.     int        pos;
  163.     char        **files;
  164. } LocalInfo;
  165.  
  166. extern void StateSetBusy(Boolean);
  167. extern char *RWGetMsg();
  168.  
  169. static void workProc(LocalInfo *l)
  170. {
  171.     extern void    GraphicOpenFile(Widget, char *, void *);
  172.     extern void    *ReadMagic(char *);
  173.     char        *file;
  174.     void        *v;
  175.  
  176.     if (l == NULL || l->id == None)
  177.         return;
  178.     XtRemoveWorkProc(l->id);
  179.     l->id = None;
  180.  
  181.     file = l->files[l->pos];
  182.  
  183.     StateSetBusy(True);
  184.     if ((v = (void *)ReadMagic(file)) != NULL) {
  185.         GraphicOpenFile(l->toplevel, file, v);
  186.     } else {
  187.         Notice(l->toplevel, "Unable to open input file \"%s\"\n    %s", file, RWGetMsg());
  188.     }
  189.     StateSetBusy(False);
  190.  
  191.     if (++l->pos == l->nfiles) {
  192.         XtFree((XtPointer)l->files);
  193.         XtFree((XtPointer)l);
  194.     } else {
  195.         l->id = XtAppAddWorkProc(XtWidgetToApplicationContext(l->toplevel), (XtWorkProc)workProc, (XtPointer)l);
  196.     }
  197. }
  198.  
  199. void main(Cardinal argc, char *argv[])
  200. {
  201.     Pixmap        icon;
  202.     XtAppContext    app;
  203.     Display        *dpy;
  204.     Widget        toplevel;
  205.     int        i;
  206.     XEvent        event;
  207.     Arg        args[5];
  208.     int        nargs = 0;
  209.     XrmDatabase    rdb;
  210.     char        *rmType;
  211.     XrmValue    rvalue;
  212.     Boolean        isIcon;
  213.  
  214.     InitTypeConverters();
  215.  
  216.     /*
  217.     **  Create the application context
  218.     */
  219.     toplevel = XtAppInitialize(&Global.appContext, "XPaint", 
  220.                 options, XtNumber(options), &argc, argv, 
  221.                 appDefaults, args, nargs);
  222.  
  223.     XtGetApplicationResources(toplevel, (XtPointer)&appInfo,
  224.                 resources, XtNumber(resources), NULL, 0);
  225.  
  226.     rdb = XtDatabase(XtDisplay(toplevel));
  227.  
  228.     if (appInfo.help != NULL)
  229.         usage(argv[0],NULL);
  230.  
  231.     if (strcmp(appInfo.visualType, "default") != 0) {
  232.         char    *cp = appInfo.visualType;
  233.         if (strncmp(cp, "cmap", 4) == 0) {
  234.             XrmPutStringResource(&rdb, "Canvas*visual", "PseudoColor");
  235.             cp+=4;
  236.         } else if (strncmp(cp, "true", 4) == 0) {
  237.             XrmPutStringResource(&rdb, "Canvas*visual", "TrueColor");
  238.             cp+=4;
  239.         } else if (strncmp(cp, "gray", 4) == 0) {
  240.             XrmPutStringResource(&rdb, "Canvas*visual", "StaticGray");
  241.             cp+=4;
  242.         } else {
  243.             usage(argv[0], "bad visual type specification");
  244.         }
  245.         if (*cp != '\0') 
  246.             XrmPutStringResource(&rdb, "Canvas*depth", cp);
  247.     }
  248.     if (argc != 1 && argv[1][0] == '-')
  249.         usage(argv[0], "Invalid option");
  250.  
  251.     /*
  252.     **  A little initilization
  253.     */
  254.     Global.region.image = NULL;
  255.     Global.region.cmap  = None;
  256.     Global.region.width = 0;
  257.     Global.region.height= 0;
  258.     Global.region.pix   = None;
  259.     Global.region.mask  = None;
  260.  
  261.     /*
  262.     **
  263.     */
  264.     XtVaGetValues(toplevel, XtNiconic, &isIcon, NULL);
  265.     if (isIcon) 
  266.         XrmPutStringResource(&rdb, "Canvas.iconic", "on");
  267.  
  268.     /*
  269.     **  Now build and construct the widgets
  270.     */
  271.     Global.timeToDie = False;
  272.     app = XtWidgetToApplicationContext(toplevel);
  273.     dpy = Global.display = XtDisplay(toplevel);
  274.  
  275.     /*
  276.     **  Call the initializers
  277.     */
  278.  
  279.     OperationInit(toplevel);
  280.  
  281.     /*
  282.     **  A few rogue initaliziers
  283.     */
  284.     BrushInit(toplevel);
  285.     HelpInit(toplevel);
  286.  
  287.     SetIconImage(toplevel, True);
  288.  
  289.     /*
  290.     **  Realize it  (doesn't hurt)
  291.     */
  292.     XtRealizeWidget(toplevel);
  293.  
  294.  
  295.     /*
  296.     **  Now open any file on the command line
  297.     */
  298.     if (argc != 1) {
  299.         LocalInfo    *l = XtNew(LocalInfo);
  300.         l->pos = 0;
  301.         l->toplevel = toplevel;
  302.         l->nfiles = argc - 1;
  303.         l->files = (char **)XtCalloc(argc, sizeof(char*));
  304.         for (i = 1; i < argc; i++)
  305.             l->files[i-1] = argv[i];
  306.         l->id = XtAppAddWorkProc(app, (XtWorkProc)workProc, (XtPointer)l);
  307.     } else if (appInfo.popped) {
  308.         /*
  309.         **  If nothing is comming up, bring up a blank canvas
  310.         */
  311.         GraphicCreate(toplevel, 0);
  312.     }
  313.  
  314.     /*
  315.     **  MainLoop
  316.     */
  317.     do {
  318.         XtAppNextEvent(app, &event);
  319.         if (event.type == ButtonPress || event.type == ButtonRelease)
  320.             Global.currentTime = event.xbutton.time;
  321.         XtDispatchEvent(&event);
  322.     } while (!Global.timeToDie);
  323. }
  324.